package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxSalesCommission;
import co.yixiang.modules.shop.repository.YxSalesCommissionRepository;
import co.yixiang.modules.shop.service.YxSalesCommissionService;
import co.yixiang.modules.shop.service.dto.YxSalesCommissionDto;
import co.yixiang.modules.shop.service.dto.YxSalesCommissionQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxSalesCommissionMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**业务员提成
 * @author frq
 * @date 2020-06-11
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxSalesCommissionServiceImpl implements YxSalesCommissionService {
    @Autowired
    private YxSalesCommissionRepository yxSalesCommissionRepository;
    @Autowired
    private YxSalesCommissionMapper yxSalesCommissionMapper;

    /* 查询数据分页
    * @param criteria 条件
    * @param pageable 分页参数
    * @return Map<String,Object>
    */
    @Override
    public Map<String, Object> queryAll(YxSalesCommissionQueryCriteria criteria, Pageable pageable) {
        Page<YxSalesCommission> page = yxSalesCommissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(yxSalesCommissionMapper::toDto));
    }
    /**
     * 查询所有数据不分页
     * @param criteria 条件参数
     * @return List<YxSalesCommissionDto>
     */
    @Override
    public List<YxSalesCommissionDto> queryAll(YxSalesCommissionQueryCriteria criteria) {
        return yxSalesCommissionMapper.toDto(yxSalesCommissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }
    /**
     * 根据ID查询
     * @param id ID
     * @return YxSalesCommissionDto
     */
    @Override
    public YxSalesCommissionDto findById(Integer id) {
        YxSalesCommission yxSalesCommission = yxSalesCommissionRepository.findById(id).orElseGet(YxSalesCommission::new);
        ValidationUtil.isNull(yxSalesCommission.getId(), "YxSalesCommission", "id", id);
        return yxSalesCommissionMapper.toDto(yxSalesCommission);
    }

    /**
     * 创建
     * @param resources /
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxSalesCommissionDto create(YxSalesCommission resources) {
        return yxSalesCommissionMapper.toDto(yxSalesCommissionRepository.save(resources));
    }
    /**
     * 导出数据
     * @param all 待导出的数据
     * @param response /
     * @throws IOException /
     */
    @Override
    public void download(List<YxSalesCommissionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxSalesCommissionDto yxSalesCommission : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("月份 格式为202005", yxSalesCommission.getMonth());
            map.put("业务员名称", yxSalesCommission.getSalesName());
            map.put("业务员头像", yxSalesCommission.getSalesAvatar());
            map.put("业务员手机号", yxSalesCommission.getSalesPhone());
            map.put("创建日期", yxSalesCommission.getAddTime());
            map.put("提成总额", yxSalesCommission.getCommission());
            map.put("投资总单数", yxSalesCommission.getOrderCount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 查询月份
     * @return
     */
    @Override
    public List<String> findMonth() {
        return yxSalesCommissionRepository.findMonth();
    }
}